import { Component, OnInit } from '@angular/core';

import { TranslateService } from '@ngx-translate/core';
import { I18nService } from './core/i18n.service';
import { ActivatedRoute, Params }  from '@angular/router';

import 'rxjs/add/operator/switchMap';
import 'rxjs/add/observable/of';

import { Observable } from 'rxjs/Observable';

@Component({
  moduleId: module.id,
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  private defaultLang = 'en-US';

  constructor (private translate:TranslateService, private i18n:I18nService, private route:ActivatedRoute) {
    translate.setDefaultLang(this.defaultLang);
  }

  ngOnInit () {
    this.route.queryParams.switchMap((params: Params) => {return Observable.of(this.i18n.getCurrentI18n(params.al, this.defaultLang))})
      .subscribe(i18n => {this.translate.use(i18n)});
  }
}
